package com.hyphenate.easeui;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hyphenate.easeui.domain.EaseEmojicon;
import com.hyphenate.easeui.domain.EaseEmojiconGroupEntity;
import com.hyphenate.easeui.model.EaseDefaultEmojiconDatas;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.easeui.widget.EaseChatExtendMenu;
import com.hyphenate.easeui.widget.EaseChatGiftMenu;
import com.hyphenate.easeui.widget.EaseChatMessageList;
import com.hyphenate.easeui.widget.emojicon.EaseEmojiconMenu;
import com.hyphenate.easeui.widget.emojicon.EaseEmojiconMenuBase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class ChatSoftInputLayout extends BaseSoftInputLayout {

    // emotionView,otherView容器
    private View container;

    private View frame;
    private EditText editText;
    private TextView btnSend;

    private View btnEmotion, btnOther, btnGift;

    private EaseChatGiftMenu giftMenu;
    private EaseChatExtendMenu extendMenu;
    private EaseEmojiconMenu emojicon;
    private ChatInputMenuListener callback;

    public ChatSoftInputLayout(Context context) {
        super(context);
    }

    public ChatSoftInputLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public ChatSoftInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ChatSoftInputLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void inflateView() {
        View layout = LayoutInflater.from(getContext()).inflate(R.layout.ease_fragment_chat_adapter, this, true);
        container = layout.findViewById(R.id.extend_container);
        frame = layout.findViewById(R.id.root_layout);
        editText = (EditText) layout.findViewById(R.id.et_sendmessage);
        btnSend = (TextView) layout.findViewById(R.id.btn_send);


        btnGift = layout.findViewById(R.id.btn_gift);
        btnGift.setOnClickListener(this);
        giftMenu = (EaseChatGiftMenu) layout.findViewById(R.id.gift_menu);
        giftMenu.setEaseChatGiftMenuItemClickListener(new EaseChatGiftMenu.EaseChatGiftMenuItemClickListener() {
            @Override
            public void onGiftClick(long giftId, String name, int value, View view) {
                if (callback != null) callback.onGiftClick(giftId, name, value, view);
            }

            @Override
            public void onChargeClick(View view) {
                if (callback != null) callback.onChargeClick(view);
            }
        });
        add2ShowViewList(giftMenu);
        add2MappingMap(btnGift, SHOW_GIFT, giftMenu);


        btnEmotion = layout.findViewById(R.id.btn_face);
        btnEmotion.setOnClickListener(this);

        emojicon = (EaseEmojiconMenu) layout.findViewById(R.id.emojicon);
        List<EaseEmojiconGroupEntity> emojiconGroupList = new ArrayList<>();
        emojiconGroupList.add(new EaseEmojiconGroupEntity(R.drawable.ee_1, Arrays.asList(EaseDefaultEmojiconDatas.getData())));
        emojicon.init(emojiconGroupList);
        emojicon.setEmojiconMenuListener(new EaseEmojiconMenuBase.EaseEmojiconMenuListener() {

            @Override
            public void onExpressionClicked(EaseEmojicon emojicon) {
                if (emojicon.getType() != EaseEmojicon.Type.BIG_EXPRESSION) {
                    if (emojicon.getEmojiText() != null) {
                        CharSequence emojiContent = EaseSmileUtils.getSmiledText(getContext(), emojicon.getEmojiText());
                        editText.append(emojiContent);
                    }
                } else {
                    if (callback != null) callback.onBigExpressionClicked(emojicon);
                }
            }

            @Override
            public void onDeleteImageClicked() {
                if (!TextUtils.isEmpty(editText.getText())) {
                    KeyEvent event = new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
                    editText.dispatchKeyEvent(event);
                }
            }
        });


        add2ShowViewList(emojicon);
        add2MappingMap(btnEmotion, SHOW_EMOTION, emojicon);// btnEmotion-(SHOW_EMOTION-emotionView)

        btnOther = layout.findViewById(R.id.btn_more);
        btnOther.setOnClickListener(this);
        extendMenu = (EaseChatExtendMenu) layout.findViewById(R.id.extend_menu);
        add2ShowViewList(extendMenu);
        add2MappingMap(btnOther, SHOW_OTHER, extendMenu);

        btnSend.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callback != null) callback.onSendMessage(editText.getText().toString());
                editText.setText("");
            }
        });

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                boolean emptyContent = editText.getText().length() == 0;
                btnSend.setVisibility(emptyContent ? View.GONE : View.VISIBLE);
                btnOther.setVisibility(emptyContent ? View.VISIBLE : View.GONE);
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });


        EaseChatMessageList easeChatMessageList = (EaseChatMessageList) layout.findViewById(R.id.message_list);
        easeChatMessageList.getListView().setOnTouchListener(new View.OnTouchListener() {
            private boolean isToHidden = false;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        isToHidden = true;
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        isToHidden = false;
                        break;
                }
                if (isToHidden) {
                    hideKeyBoardView();
                    isToHidden = false;
                }
                return false;
            }
        });

    }


    @Override
    protected View getContainer() {
        return container;
    }

    @Override
    protected View getFrame() {
        return frame;
    }

    @Override
    public EditText getEditText() {
        return editText;
    }

    @Override
    protected View getBtnKeyBoard() {
        return null;
    }


    public void registerExtendMenuItem(int nameRes, int drawableRes, int itemId,
                                       EaseChatExtendMenu.EaseChatExtendMenuItemClickListener listener) {
        extendMenu.registerMenuItem(nameRes, drawableRes, itemId, listener);
    }

    public void initExtendMenuItem() {
        extendMenu.init();
    }

    public void setChatInputMenuListener(ChatInputMenuListener listener) {
        this.callback = listener;
    }

    public void initGiftMenu(float balance) {
        giftMenu.setBalance(balance);
    }



    public interface ChatInputMenuListener {
        /**
         * 发送消息按钮点击
         *
         * @param content 文本内容
         */
        void onSendMessage(String content);

        /**
         * 大表情被点击
         *
         * @param emojicon
         */
        void onBigExpressionClicked(EaseEmojicon emojicon);

        /**
         * 长按说话按钮touch事件
         *
         * @param v
         * @param event
         * @return
         */
        boolean onPressToSpeakBtnTouch(View v, MotionEvent event);

        // 选择了一个礼物
        void onGiftClick(long giftId, String name, int value, View v);

        // 点击了充值
        void onChargeClick(View v);

    }

}
